#ifndef UTI_DB_H_
#define UTI_DB_H_
#pragma once
#include "sys_config.h"
#include "uti_barseries_broad.h"

#include "KompexSQLitePrerequisites.h"
#include "KompexSQLiteDatabase.h"
#include "KompexSQLiteStatement.h"
#include "KompexSQLiteException.h"
#include "KompexSQLiteStreamRedirection.h"
#include "KompexSQLiteBlob.h"

#include <iostream>
#include <exception>
using namespace Kompex;
#pragma warning(disable : 4996)

class DB
{
public:

	//DB(BarInfo* pbar, BarSeries_broad** ppbarserbroad, unsigned int cnt);

	DB::DB(string stringInst, BarSeries_broad** ppbarserbroad, unsigned int cnt);

	~DB();

	bool WriteData();

private:


	bool InitDBInfo(BarSeries_broad** ppbarserbroad, unsigned int cnt);

	void DestroyDBInfo();

	bool InitBuffer();

	void DestroyBuffer();

	bool CheckDBFile();

	bool OpenDB(unsigned int index);

	bool WriteDB(unsigned int index);

	bool CloseDB(unsigned int index);

	bool UpdateDB(unsigned int index);

	bool InsertDB(unsigned int index);

	bool UpdateCSV(unsigned int index);

	bool InsertCSV(unsigned int index);

	bool UpdateMemDB(unsigned int index);


	

	bool OnFireConditon();

	bool SetDBLastbar(unsigned int index);



	bool InsertDB_TEST(unsigned int index, unsigned int base);
public:

	//BarInfo m_barinfo;

	string m_stringInst;

private:

	typedef struct DBINFO
	{
		//DBINFO(char* _dbname, char* _tbname, char* _csvname, char* _dbf, char* _csvf, BarSeries_broad* _pbarserbroad)
		DBINFO(string& _dbname, string& _tbname, string& _csvname, string& _dbf, string& _csvf, BarSeries_broad* _pbarserbroad)
		{

			pdb = NULL;
			pst = NULL;

			dbname = _dbname;
			tbname = _tbname;
			csvname = _csvname;
			dbfolder =  _dbf;
			csvfolder =  _csvf;


			pbarserbroad = _pbarserbroad;

			
		};

		string dbname;
		string tbname;
		BarSeries_broad* pbarserbroad;

		SQLiteDatabase* pdb;
		SQLiteStatement* pst;

		string csvname;		

		string dbfolder;
		string csvfolder;


	}DBINFO;


	vector<DBINFO*> m_vpdbinfo;

	char* m_buffer;

	


	
};





#endif